package com.ljl.mvvmdemo.util

import com.google.gson.Gson
import java.lang.reflect.Type

/**
 * created by luojialun on 2024/10/23
 */
class GsonUtils {

    companion object {
        @Volatile
        private var instance: GsonUtils? = null

        fun getInstance(): GsonUtils {
            return instance ?: synchronized(this) {
                instance ?: GsonUtils().also { instance = it }
            }
        }
    }

    private var gson: Gson = Gson()

    fun toJson(clazz: Any): String {
        return gson.toJson(clazz)
    }

    fun <T> fromJson(json: String, clz: Class<T>): T {
        return gson.fromJson(json, clz)
    }

    fun <T> fromJson(json: String, type: Type): T {
        return gson.fromJson(json, type)
    }

}